package com.transaction.stockseata.control;

import com.transaction.stockseata.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ausu
 * @version 1.0
 * @description: TODO
 * @date 2023/1/7 18:11
 */
@RestController
@RequestMapping("/stock")
public class StockControl {

    @Autowired
    private StockService stockService;

    @GetMapping("/deductStock")
    public String deductStock(@RequestParam("productId") Integer productId) {
        return stockService.deduct(productId);
    }

    /**
     * 如果通过IO流来解析参数，默认情况下，IO流读一次就结束了，就没有了。
     * 而有些场景需要读取多次参数。
     *
     * @RequestBody注解，这个底层也是通过ID流来读取数据的，
     * 但是由于IO流在拦截器中已经被读取过一次了，所以到了接口中在去读取就会报错
     *
     * java.lang.IllegalStateException: getReader() has already been called for this request
     *
     * @param msg
     */
    @PostMapping("/hello")
    public void hello(@RequestBody String msg) {
        System.out.println("msg  : " + msg);
    }
}
